package cn.wjh.business.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import cn.wjh.business.domain.Works;
import cn.wjh.business.service.IWorksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品管理Controller
 *
 * @author jiraWu
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/business/works")
public class WorksController extends BaseController {
	@Resource
	private IWorksService worksService;

	/**
	 * 查询作品管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:works:list')")
	@GetMapping("/list")
	public TableDataInfo list(Works works) {
		startPage();
		List<Works> list = worksService.list(new QueryWrapper<>(works));
		return getDataTable(list);
	}

	/**
	 * 导出作品管理列表
	 */
	@PreAuthorize("@ss.hasPermi('business:works:export')")
	@Log(title = "作品管理", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Works works) {
		List<Works> list = worksService.list(new QueryWrapper<>(works));
		ExcelUtil<Works> util = new ExcelUtil<Works>(Works.class);
		util.exportExcel(response, list, "作品管理数据");
	}

	/**
	 * 获取作品管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:works:query')")
	@GetMapping(value = "/{worksId}")
	public AjaxResult getInfo(@PathVariable("worksId") Long worksId) {
		return AjaxResult.success(worksService.getById(worksId));
	}

	/**
	 * 新增作品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:works:add')")
	@Log(title = "作品管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Works works) {
		works.setCreateBy(getUsername());
		works.setCreateTime(DateUtils.getNowDate());
		return toAjax(worksService.save(works));
	}

	/**
	 * 修改作品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:works:edit')")
	@Log(title = "作品管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Works works) {
		works.setUpdateBy(getUsername());
		works.setUpdateTime(DateUtils.getNowDate());
		return toAjax(worksService.updateById(works));
	}

	/**
	 * 删除作品管理
	 */
	@PreAuthorize("@ss.hasPermi('business:works:remove')")
	@Log(title = "作品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{worksIds}")
	public AjaxResult remove(@PathVariable List<Long> worksIds) {
		return toAjax(worksService.removeByIds(worksIds));
	}
}

